package jfox.platform.datadict.dao;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;

import jfox.platform.datadict.entity.DictCategory;
import jfox.platform.infrastructure.EntityObject;
import jfox.platform.infrastructure.SuperDAO;

/**
 * @author <a href="mailto:yang_y@sysnet.com.cn">Young Yang</a>
 */
@NamedNativeQueries(
        {
        @NamedNativeQuery(
                name = DictCategoryDAOBean.GET_CATEGORY_BY_ID,
                query = "SELECT * FROM T_SYS_DICT_CATEGORY WHERE ID=$ID",
                resultClass = DictCategory.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                }),
        @NamedNativeQuery(
                        name = DictCategoryDAOBean.GET_CATEGORY_BY_CODE,
                        query = "SELECT * FROM T_SYS_DICT_CATEGORY WHERE CODE=$CODE",
                        resultClass = DictCategory.class,
                        hints = {
                        	@QueryHint(name = "cache.partition", value = "dictmgmt")
                        }),                
        @NamedNativeQuery(
                name = DictCategoryDAOBean.GET_ALL_CATEGORY,
                query = "SELECT * FROM T_SYS_DICT_CATEGORY WHERE VSTATUS=0 ORDER BY NAME",
                resultClass = DictCategory.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                }),                    
        @NamedNativeQuery(
                name = DictCategoryDAOBean.INSERT_CATEGORY,
                query = "INSERT INTO T_SYS_DICT_CATEGORY(" +
                		"ID, " +
                		"NAME, " +
                		"LOCAL_NAME, " +
                		"PRIORITY, " +
                		"VSTATUS, " +
                		"CREATE_TIME, " +
                		"CREATOR, " +
                		"LAST_MODIFIED, " +
                		"LAST_MODIFIER, " +
                		"DESCRIPTION, " +
                		"PARENT_ID," +
                		"REFS," +
                		"VERSION," + 
                		"CODE) " +
                		"VALUES(" +
                		"$CATEGORY.getId()," +
                        "$CATEGORY.getName()," +
                        "$CATEGORY.getLocalName()," +
                        "$CATEGORY.getPriority()," +
                        "$CATEGORY.getVstatus()," +
                        "$CATEGORY.getCreateTime()," +
                        "$CATEGORY.getCreator()," +
                        "$CATEGORY.getLastModified()," +
                        "$CATEGORY.getLastModifier()," +                        
                        "$CATEGORY.getDescription()," +
                        "$CATEGORY.getParentId()," +
                        "$CATEGORY.getRefs()," +
                        "$CATEGORY.getVersion()," +                        
                        "$CATEGORY.getCode())",
                resultClass = DictCategory.class,
                hints = {
                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                }),      
                
                @NamedNativeQuery(
                        name = DictCategoryDAOBean.UPDATE_CATEGORY,
                        query = "UPDATE T_SYS_DICT_CATEGORY SET " +
                        		"NAME=$CATEGORY.getName(), " +
                        		"LOCAL_NAME=$CATEGORY.getLoacalName, " +
                        		"PRIORITY=$CATEGORY.getPriority(), " +
                        		"VSTATUS=$CATEGORY.getVstatus(), " +
                        		"CREATE_TIME=$CATEGORY.getCreateTime(), " +
                        		"CREATOR=$CATEGORY.getCreator(), " +
                        		"LAST_MODIFIED=$CATEGORY.getLastModiied(), " +
                        		"LAST_MODIFIER=$CATEGORY.getLastModifier(), " +
                        		"DESCRIPTION=$CATEGORY.getDescription(), " +
                        		"PARENT_ID=$CATEGORY.getParentId()," +
                        		"REFS=$CATEGORY.getRefs()," +
                        		"VERSION=$CATEGORY.getVersion()," +                        		
                        		"CODE=$CATEGORY.getCode()  " +
                        		"WHERE ID=$CATEGORY.getId()",
                        resultClass = DictCategory.class,
                        hints = {
                        	@QueryHint(name = "cache.partition", value = "dictmgmt")
                        }),            
                 @NamedNativeQuery(
                                name = DictCategoryDAOBean.DELETE_CATEGORY,
                                query = "UPDATE T_SYS_DICT_CATEGORY SET VSTATUS = " + EntityObject.VSTATUS_REMOVED + " WHERE ID=$ID",
                                resultClass = DictCategory.class,
                                hints = {
                                	@QueryHint(name = "cache.partition", value = "dictmgmt")
                                }      

                        )                        
                }        
        
)
@Stateless
@Local
public class DictCategoryDAOBean extends SuperDAO implements DictCategoryDAO {

    public final static String GET_CATEGORY_BY_ID ="DictCategoryDAOBean.getCategory";//系统不能区分重名
    public final static String GET_CATEGORY_BY_CODE ="DictCategoryDAOBean.getCategoryByCode";//系统不能区分重名
    public final static String GET_ALL_CATEGORY = "DictCategoryDAOBean.getAllCategory";
    public final static String INSERT_CATEGORY = "DictCategoryDAOBean.insertCategory";
    public final static String DELETE_CATEGORY = "DictCategoryDAOBean.deleteCategory";
    public final static String UPDATE_CATEGORY = "DictCategoryDAOBean.updateCategory"; 

    public DictCategory getCategoryById(long id) {
        return (DictCategory)getEntityObjectByColumn(GET_CATEGORY_BY_ID, "ID", id);        
    }
    
    public DictCategory getCategoryByCode(String code) {
        return (DictCategory)getEntityObjectByColumn(GET_CATEGORY_BY_CODE, "CODE", code);        
    }    

    public List<DictCategory> getAllCategory() {
        return (List<DictCategory>)processNamedNativeQuery(GET_ALL_CATEGORY, Collections.EMPTY_MAP);
    }
    
    public void insertCategory(DictCategory dictCategory) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("CATEGORY", dictCategory);
        executeNamedNativeUpdate(this.INSERT_CATEGORY, params);
    }

    public int deleteCategory(long id) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("ID", id);
        return executeNamedNativeUpdate(this.DELETE_CATEGORY, params);
    }

    public int updateCategory(DictCategory dictCategory) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("CATEGORY", dictCategory);
        return executeNamedNativeUpdate(this.UPDATE_CATEGORY, params);
    }    
    
}
